<template>
  <basic-container>
    <avue-form v-if="viewVisible"
               style="height: 100%; overflow: auto;"
               ref="viewForm"
               v-model="viewForm"
               :option="viewOption"
               @submit="submit">
    </avue-form>
  </basic-container>
</template>

<script>

import { getForm, saveFormData, getFormData } from "@/api/plugin/form/form-view";

import { mapGetters } from "vuex";

export default {
  data() {
    return {
      viewVisible: false,
      viewForm: {},
      viewOption: {},
      formCode: null,
      version: null,
      menuBtn: null,
      rowId: null,
      dataId: null
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.design_form_add, false),
        viewBtn: this.vaildData(this.permission.design_form_view, false),
        delBtn: this.vaildData(this.permission.design_form_delete, false),
        editBtn: this.vaildData(this.permission.design_form_edit, false)
      };
    }
  },
  mounted() {
    this.handleFormInfo();
    this.handleFormData();
  },
  methods: {
    submit() {
      let row = {"formCode":this.formCode,"version":this.version,"rowId":this.rowId,"content":JSON.stringify(this.viewForm)};
      if(this.dataId){
        row.id = this.dataId;
      }
      saveFormData(row).then(()=>{
        this.$message({
          type: "success",
          message: "操作成功!"
        });
      },error => {
        console.log(error);
      });
    },
    handleFormInfo() {
      this.formCode = this.$route.params.formCode;
      this.version = this.$route.params.version;
      this.menuBtn = this.$route.params.menuBtn; //1显示 0不显示
      this.rowId = this.$route.params.rowId;
      getForm(this.formCode,this.version).then(res=>{
        let data = res.data.data;
        this.viewOption = { ...eval('(' + data.content + ')'), menuBtn: this.menuBtn==1?true:false }; //, detail:true
        this.viewVisible = true
        setTimeout(() => {
          this.$refs.viewForm.clearValidate()
        })
      }).catch(res=>{
        console.log(res);
      });
    },
    handleFormData() {
      getFormData(this.formCode,this.version,this.rowId).then(res=>{
        let data = res.data.data;
        this.dataId = data.id;
        this.viewForm = eval('(' + data.content + ')');
      },error => {
        console.log(error);
      });
    }
  }
};
</script>

<style>
</style>
